<template>
  <div>
    <a href="">&lt;返回</a>
    <p class="title">不凡</p>
    <input type="text" placeholder="搜索" v-model="ipt" />
    <ul>
      <li v-for="item in list" :key="item.uid">
        {{ item.title }}
        {{ item.address }}
      </li>
    </ul>
    <h2>显示当前位置:</h2>
    <div id="allmap"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ipt: "",
      map: "",
      list: []
    };
  },
  watch: {
    ipt() {
      var that = this;

      //watch箭头函数里面的this指向会改变
      var options = {
        onSearchComplete: function(results) {
          // 判断状态是否正确
          if (local.getStatus() == BMAP_STATUS_SUCCESS) {
            // console.log(results.Hr);

            that.list = results.Hr;
            // console.log(that.list);
          }
        }
      };

      //箭头函数外边的this指向不变
      var local = new BMap.LocalSearch(this.map, options);
      local.search(this.ipt);
    }
  },
  computed: {},
  created() {},
  mounted() {
    var map = new BMap.Map("allmap");
    this.map = map;
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);

    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(
      function(r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
          var mk = new BMap.Marker(r.point);
          map.addOverlay(mk);
          map.panTo(r.point);
        } else {
          alert("failed" + this.getStatus());
        }
      },
      { enableHighAccuracy: true }
    );
  },
  methods: {}
};
</script>

<style scoped lang="scss">
#allmap {
  height: 250px;
  margin-bottom: 0px;
}
.title {
  font-size: 16px;
  color: brown;
}
a {
  text-decoration: none;
  color: brown;
  font-size: 14px;
  position: absolute;
  left: 20px;
  top: 20px;
}
.span {
  margin-left: 20px;
}
h2 {
  position: absolute;
  left: 20px;
  top: 350px;
}
input {
  width: 300px;
  height: 40px;
  border: 2px solid lightgray;
  border-radius: 5px;
}
ul {
  height: 320px;
}
li {
  margin-bottom: 10px;
  font-size: 12px;
  margin-left: 2px;
}
</style>